home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #4 / Amiga Plus CD - 2000 - No. 4.iso / PowerPC / Dev / PPCRelease / Examples / R.Mainz / fastprim / fastprim.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-28  |  1.1 KB  |  78 lines

  1.  
  2. /*
  3. **
  4. ** $VER: fastprim.c 1.0 (6.9.1995)
  5. **
  6. ** Written by Roland Mainz, 1995
  7. **
  8. */
  9.  
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <math.h>
  14.  
  15. #define TEST_TEST 1
  16.  
  17. #define ARRAY_SIZE (1000000)
  18.  
  19. unsigned long p[ ARRAY_SIZE ];
  20.  
  21.  
  22. int main( void )
  23. {
  24.     unsigned long i,
  25.                   j,
  26.                   checkmax,
  27.                   isprim;
  28.     unsigned long pa;
  29.  
  30.     pa = 0U;
  31.     p[ pa ] = 2U; /* first entry */
  32.  
  33.     for( i = 3U ; pa < ARRAY_SIZE ; (i+= 2U) )
  34.     {
  35.       checkmax = ((unsigned short int)sqrt( (double)i )) + 1U;
  36.  
  37.       isprim = 1U;
  38.  
  39.       for( j = 0 ; j < pa ; j++ )
  40.       {
  41.         if( p[ j ] > checkmax )
  42.         {
  43.           break;
  44.         }
  45.  
  46.         if( (i % p[ j ]) == 0U )
  47.         {
  48.           isprim = 0U;
  49.           break;
  50.         }
  51.       }
  52.  
  53.       if( isprim == 1U )
  54.       {
  55. #ifdef TEST_TEST
  56.         static long strip_out = 0L;
  57. #endif /* TEST_TEST */
  58.  
  59.         pa++;
  60.  
  61.         p[ pa ] = i;
  62.  
  63. #ifdef TEST_TEST
  64.         if( (strip_out++ % 1000L) == 0L )
  65. #endif /* TEST_TEST */
  66.  
  67.         printf( "%ld\n", i );
  68.       }
  69.     }
  70.  
  71. #ifdef TEST_TEST
  72.     printf( "Done !\n" );
  73. #endif /* TEST_TEST */
  74.  
  75.     return( EXIT_SUCCESS );
  76. }
  77.  
  78.